﻿using System;
using Mani.Patient.Business.Domain.Entity;
using Mani.Patient.Business.Services;
using System.Web.UI.WebControls;
using System.Web.UI;

public partial class visitd : System.Web.UI.Page {

    public IPatientService PatientService { get; set; }

    protected override void InitializeCulture() {
        MyCulture.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            Patient p = PatientService.GetPatientById(Convert.ToInt32(Request["Id"]));
            lbPatientName.Text = p.PatientName;
            lbPatientName.PostBackUrl = "PatientInfo.aspx?action=edit&Id=" + p.Id;
            LoadFileInfo();
            LoadVisitsHistory(0, true);
        }
    }

    private void LoadFileInfo() {
        Patient p = PatientService.GetPatientById(Convert.ToInt32(Request["Id"]));
        rQuestions.DataSource = p.File.Answers;
        rQuestions.DataBind();
    }

    private void LoadVisitsHistory(int pageNumber, bool setPageCount)
    {
        Patient p = PatientService.GetPatientById(Convert.ToInt32(Request["Id"]));

        if (setPageCount)
            gvVisitHistory.VirtualItemCount = PatientService.GetCountVisitHistory(p);

        gvVisitHistory.DataSource = PatientService.GetVisitHistory(p, gvVisitHistory.PageSize, pageNumber);
        gvVisitHistory.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e) {

        Visit v = new Visit();
        v.Description = txtVisit.Text;
        v.Patient = PatientService.GetPatientById(Convert.ToInt32(Request["Id"]));
        v.VisitDate = DateTime.Now;
        PatientService.SubmitVisit(v);
        txtVisit.Text = "";
        LoadVisitsHistory(0,true);
    }

    protected void gvVisitHistory_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        gvVisitHistory.PageIndex = e.NewPageIndex;
        LoadVisitsHistory(e.NewPageIndex, false);
    }

    protected void gvVisitHistory_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gvVisitHistory.DataKeys[e.RowIndex].Value);
        PatientService.DeleteVisitHistory(id);
        LoadVisitsHistory(0, true);
    }

    protected void gvVisitHistory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        Control deleteButton = e.Row.FindControl("lbDeleteVisit");

        if (deleteButton != null)
        {
            string message = GetLocalResourceObject("DeleteMessage").ToString();
            ((LinkButton)deleteButton).OnClientClick = String.Format("javascript:return confirm('{0}');", message);
        }
    }
}
